<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/11/27
 * Time: 19:58
 */

class Mysql
{
    // mysql 连接对象
    public $dbSource = '';
    //mysql 配置
    public $dbConfig = [];


    public function __construct()
    {
        $this->dbSource = new swoole_mySQL();
        $this->dbConfig = [
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'password' => 'OKI^4&TwVfdQQ0SK',
            'database' => 'swoole',
            'charset' => 'utf8', //指定字符集
            'timeout' => 2,  // 可选：连接超时时间（非查询超时时间），默认为SW_MYSQL_CONNECT_TIMEOUT（1.0）
        ];
    }

    public function execute($id, $username)
    {
        $this->dbSource->connect($this->dbConfig, function ($db, $result){
            if ($result === false) {
                var_dump($db->connect_error);
            }

            $sql = "select * from test where id=1";
            $db->query($sql, function ($db, $result){
                if ($result === false) {

                } elseif ($result === true) {

                } else {
                    var_dump($result);
                }
                $db->close();
            });
        });
        return true;
    }
}

$obj = new Mysql();
$obj->execute(1, 'sdfs');